package com.example.prison.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.prison.entity.AreaManagement;
import com.example.prison.entity.vo.AreaManagementVO;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
* @author jiang
* @description 针对表【area_management】的数据库操作Mapper
* @createDate 2023-05-26 17:15:28
* @Entity com.example.prison.entity.AreaManagementVO
*/
@Mapper
public interface AreaManagementMapper extends BaseMapper<AreaManagement> {

    @Select("<script>select * from area_management " +
            " <where> " +
            " <if test=\"defenseSection != null and defenseSection != ''\">and defense_section like concat('%',#{defenseSection},'%') </if> " +
            " <if test=\"defenseSectionPoint != null and defenseSectionPoint != ''\">and defense_section_point like concat('%',#{defenseSectionPoint},'%') </if> " +
            " <if test=\"sentryPost != null and sentryPost != ''\">and sentry_post like concat('%',#{sentryPost},'%') </if> " +
            " <if test=\"alarmStatus != null and alarmStatus != ''\">and alarm_status like concat('%',#{alarmStatus},'%') </if> " +
            " </where> " +
            "order by id desc</script>")
    List<AreaManagement> get(@Param("defenseSection") String defenseSection, @Param("defenseSectionPoint") String defenseSectionPoint,
                             @Param("sentryPost") String sentryPost,@Param("alarmStatus") String alarmStatus);

    @Select("<script>select * from area_management order by id desc</script>")
    List<AreaManagement> list();

    @Select("select id,defense_section as number,defense_address as address,status from area_management order by id desc")
    List<AreaManagementVO> getDefenceArea();


    @Select("select * from area_management a where a.sentry_post is not null order by a.id desc")
    List<AreaManagement> getSentryPost();

    @Insert("insert into area_management(defense_section,defense_section_point,defense_address,sentry_post,sentry_address,time,status,voltageA,currentA,voltageB,currentB," +
            "voltageC,currentC,high_rated_voltage,rated_current,rated_voltage,alarm_level,alarm_status,device_number,gateway_address,gateway_port,module_address,detection," +
            "linear_voltageAB,linear_voltageAC,linear_voltageBC,is_mark,statusA,statusB,statusC,setting) " +
            "values (#{defenseSection},#{defenseSectionPoint},#{defenseAddress},#{sentryPost},#{sentryAddress},#{time},#{status},#{voltageA},#{currentA},#{voltageB},#{currentB}," +
            "#{voltageC},#{currentC},#{highRatedVoltage},#{ratedCurrent},#{ratedVoltage},#{alarmLevel},#{alarmStatus},#{deviceNumber},#{gatewayAddress},#{gatewayPort}," +
            "#{moduleAddress},#{detection},#{linearVoltageAB},#{linearVoltageAC},#{linearVoltageBC},#{isMark},#{statusA},#{statusB},#{statusC},#{setting})")
    int add(AreaManagement area);

    @Update("update area_management set defense_section=#{area.defenseSection}, defense_section_point=#{area.defenseSectionPoint}, defense_address=#{area.defenseAddress}, " +
            "sentry_post=#{area.sentryPost}, sentry_address=#{area.sentryAddress}, time=#{area.time}, status=#{area.status}, voltageA=#{area.voltageA},currentA=#{area.currentA}," +
            "voltageB=#{area.voltageB},currentB=#{area.currentB},voltageC=#{area.voltageC},currentC=#{area.currentC}, high_rated_voltage=#{area.highRatedVoltage}, " +
            "rated_voltage=#{area.ratedVoltage}, rated_current=#{area.ratedCurrent}, alarm_level=#{area.alarmLevel}, alarm_status=#{area.alarmStatus}, device_number=#{area.deviceNumber}," +
            "gateway_address=#{area.gatewayAddress},gateway_port=#{area.gatewayPort},module_address=#{area.moduleAddress}, detection=#{area.detection},linear_voltageAB=#{area.linearVoltageAB}," +
            "linear_voltageAC=#{area.linearVoltageAC},linear_voltageBC=#{area.linearVoltageBC}, statusA=#{area.statusA}, statusB=#{area.statusB}, statusC=#{area.statusC} " +
            "where id = #{id}")
    int updateArea(AreaManagement area,Integer id);

    @Delete("<script>delete from area_management where id in <foreach collection=\"ids\" item=\"item\" open=\"(\" separator=\",\" close=\")\">#{item} </foreach> </script>")
    void deleteArea(@Param("ids") Integer[] ids);

    @Delete("delete from area_management where id=#{id}")
    void deleteOneById(@Param("id") Integer id);

    @Delete("delete from area_management")
    void deleteAll();

    @Delete("delete from area_management where defense_section = #{defenseSection}")
    void deleteAreaBySection(@Param("defenseSection") String defenseSection);

    @Select("select * from area_management where id=#{id}")
    AreaManagement getById(@Param("id") Integer id);

    @Select("select * from area_management a where a.defense_section like concat('%',#{defenseSection},'%') order by a.id desc")
    List<AreaManagement> getBySection(@Param("defenseSection") String defenseSection);

    @Select("select * from area_management a where a.defense_section=#{defenseSection}")
    AreaManagement getOneByDefenseSection(@Param("defenseSection") String defenseSection);

    @Select("select * from area_management a where a.defense_section=#{defenseSection}")
    List<AreaManagement> getByDefenseSection(@Param("defenseSection") String defenseSection);

    @Select("select * from area_management a where a.is_mark='未标记'")
    List<AreaManagement> getUnmarkArea();

    @Update("update area_management set is_mark='已标记',mark_id=#{MarkId} where defense_section=#{defenseSection} ")
    void setMark(@Param("defenseSection") String defenseSection, @Param("MarkId") String MarkId);

    @Update("update area_management set is_mark='未标记' where defense_section=#{defenseSection} ")
    void setUnMark(@Param("defenseSection") String defenseSection);

    @Update("update area_management set gateway_address=#{ip},gateway_port=#{port}")
    int updateIPAndPort(@Param("ip")String ip, @Param("port")Integer port);

    @Update("update area_management set setting=#{setting} where module_address=#{moduleAddress}")
    int updateSetting(@Param("setting")String setting, @Param("moduleAddress")String moduleAddress);

}




